<template>
	<view class="upload_box">
		<view class="front_box" @click="handleFront">
			<image :src="frontImg" mode="widthFix"></image>
		</view>

		<view class="back_box" @click="handleBack">
			<image :src="backImg" mode="widthFix"></image>
		</view>

		<view class="submit_btn" @click="handleSubmit">
			提交
		</view>

		<view class="upload_tip">
			提示：根据相关法律法规，用户申请提现需实名认证。请务保证实名身份证与收款账号、手机号归属于同一个人，否则将打款失败。
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				frontImg: 'https://file-1314054542.cos.ap-shanghai.myqcloud.com/cos_file/wechat_xcx/66a9ead093ac6_1722411728.png',
				backImg: 'https://file-1314054542.cos.ap-shanghai.myqcloud.com/cos_file/wechat_xcx/66a9eae5db5bc_1722411749.png'
			}
		},
		methods: {
			// 提交实名认证接口
			realName() {
				this.$request('/api/realName', {
					id_card_front: this.frontImg,
					id_card_back: this.backImg
				}, 'POST').then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '实名认证成功',
							icon: 'none',
							success: res => {
								setTimeout(() => {
									uni.redirectTo({
										url: '/pages/my/withdrawal/withdrawal'
									})
								}, 1200)
							}
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}
				})
			},

			// 上传正面照
			handleFront() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'https://saler.jkcae.com/app/api/uploadFilesCompress', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								'authorization': uni.getStorageSync('h5-token'),
								'platform': 1
							},
							formData: {
								'type': '14'
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data)
								if (res.code == 1) {
									this.frontImg = res.data.path
								} else {
									uni.showToast({
										title: '上传失败',
										icon: 'none'
									})
									return
								}
							}
						});
					}
				});
			},

			// 上传背面照
			handleBack() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'https://saler.jkcae.com/app/api/uploadFilesCompress', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								'authorization': uni.getStorageSync('h5-token'),
								'platform': 1
							},
							formData: {
								'type': '14'
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data)
								if (res.code == 1) {
									this.backImg = res.data.path
								} else {
									uni.showToast({
										title: '上传失败',
										icon: 'none'
									})
									return
								}
							}
						});
					}
				});
			},

			// 提交
			handleSubmit() {
				if (this.frontImg == '/static/my/img-front.png') {
					uni.showToast({
						title: '请上传身份证正面照',
						icon: 'none'
					})
					return
				} else if (this.backImg == '/static/my/img-back.png') {
					uni.showToast({
						title: '请上传身份证反面照',
						icon: 'none'
					})
					return
				} else {
					this.realName()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.upload_box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 100rpx 0;

		.front_box {
			margin-bottom: 70rpx;
		}

		.upload_tip {
			color: #999;
			padding: 60rpx 50rpx 0;
		}

		.submit_btn {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 635rpx;
			height: 84rpx;
			background: #4889FF;
			border-radius: 42rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 34rpx;
			color: #FFFFFF;
			margin-top: 40rpx;
		}
	}
</style>